package it.infodea.tapestrydea.services.jcr.impl;

import it.infodea.tapestrydea.services.jcr.IJcrConstants;
import it.infodea.tapestrydea.services.jcr.RepositoryAccessService;

import javax.jcr.Repository;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Symbol;

public class RepositoryAccessServiceImpl implements RepositoryAccessService {

	private String repositoryName;
	private String jndiProvider;
	
	
	public RepositoryAccessServiceImpl(
			@Inject @Symbol(IJcrConstants.MEDIS_JCR_REPOSITORY_NAME) String repositoryName, 
			@Inject @Symbol(Context.PROVIDER_URL) String jndiProvider) {
		super();
		this.repositoryName = repositoryName;
		this.jndiProvider = jndiProvider;
	}


	public Repository getRepository() {
		InitialContext ctx;
		Repository r = null;
		try {
			ctx = new InitialContext(IJcrConstants.provideJndiEnv(jndiProvider));
			r =  (Repository)ctx.lookup(repositoryName);
		} catch (NamingException e) {
			System.out.println("Errore durante l'accesso al jndi");
			e.printStackTrace();
		}
		return r;
	}

}
